package com.anonymous.community.ui.home;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.anonymous.community.R;
import com.anonymous.community.api.model.CommentTreeResponse;

import java.util.ArrayList;
import java.util.List;

/**
 * 评论适配器
 * 支持树形结构展示
 */
public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.CommentViewHolder> {

    private List<CommentTreeResponse.CommentItem> comments;
    private OnCommentInteractionListener listener;

    public interface OnCommentInteractionListener {
        void onCommentLikeClick(int commentId);
        void onCommentReplyClick(int commentId, String username);
    }

    public CommentAdapter() {
        this.comments = new ArrayList<>();
    }

    public void setOnCommentInteractionListener(OnCommentInteractionListener listener) {
        this.listener = listener;
    }

    public void setComments(List<CommentTreeResponse.CommentItem> comments) {
        this.comments = flattenComments(comments);
        notifyDataSetChanged();
    }

    public void clearComments() {
        this.comments.clear();
        notifyDataSetChanged();
    }

    /**
     * 将树形评论结构展平为列表
     */
    private List<CommentTreeResponse.CommentItem> flattenComments(List<CommentTreeResponse.CommentItem> comments) {
        List<CommentTreeResponse.CommentItem> result = new ArrayList<>();
        if (comments != null) {
            for (CommentTreeResponse.CommentItem comment : comments) {
                result.add(comment);
                // 递归添加回复
                if (comment.getReplies() != null && !comment.getReplies().isEmpty()) {
                    result.addAll(flattenComments(comment.getReplies()));
                }
            }
        }
        return result;
    }

    @NonNull
    @Override
    public CommentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_comment, parent, false);
        return new CommentViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CommentViewHolder holder, int position) {
        CommentTreeResponse.CommentItem comment = comments.get(position);
        holder.bind(comment, position);
    }

    @Override
    public int getItemCount() {
        return comments.size();
    }

    class CommentViewHolder extends RecyclerView.ViewHolder {
        private TextView tvUsername;
        private TextView tvContent;
        private TextView tvTime;
        private TextView tvLikeCount;
        private ImageView ivLike;
        private LinearLayout layoutLike;
        private LinearLayout layoutReply;

        public CommentViewHolder(@NonNull View itemView) {
            super(itemView);
            tvUsername = itemView.findViewById(R.id.tv_username);
            tvContent = itemView.findViewById(R.id.tv_content);
            tvTime = itemView.findViewById(R.id.tv_time);
            tvLikeCount = itemView.findViewById(R.id.tv_like_count);
            ivLike = itemView.findViewById(R.id.iv_like);
            layoutLike = itemView.findViewById(R.id.layout_like);
            layoutReply = itemView.findViewById(R.id.layout_reply);
        }

        public void bind(CommentTreeResponse.CommentItem comment, int position) {
            // 设置用户名
            tvUsername.setText(comment.getAnonymousId());

            // 设置内容
            tvContent.setText(comment.getContent());

            // 设置时间
            tvTime.setText(formatTime(comment.getCreatedAt()));

            // 设置点赞数
            tvLikeCount.setText(String.valueOf(comment.getLikeCount()));

            // 设置点赞状态
            if (comment.isLiked()) {
                ivLike.setImageResource(R.drawable.ic_like_filled);
                ivLike.setColorFilter(itemView.getContext().getResources().getColor(R.color.black));
            } else {
                ivLike.setImageResource(R.drawable.ic_like_outline);
                ivLike.setColorFilter(itemView.getContext().getResources().getColor(R.color.colorGray));
            }

            // 点赞点击
            layoutLike.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onCommentLikeClick(comment.getId());
                }
            });

            // 回复点击
            layoutReply.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onCommentReplyClick(comment.getId(), comment.getAnonymousId());
                }
            });
        }

        private String formatTime(String createdAt) {
            // TODO: 实现时间格式化
            return createdAt;
        }
    }
}

